/*  Copyright (c) 2010 Xiaoyun Zhu
 * 
 *  Permission is hereby granted, free of charge, to any person obtaining a copy  
 *  of this software and associated documentation files (the "Software"), to deal  
 *  in the Software without restriction, including without limitation the rights  
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 *  copies of the Software, and to permit persons to whom the Software is  
 *  furnished to do so, subject to the following conditions:
 *  
 *  The above copyright notice and this permission notice shall be included in  
 *  all copies or substantial portions of the Software.
 *  
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  
 *  THE SOFTWARE.  
 */
package cn.kk.exia;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

public class ProxyPanel extends javax.swing.JPanel {
	private static final long	serialVersionUID	= 4558328191741920665L;
	private int	action	= -1;
	private JDialog parent;

	/**
	 * Creates new form ProxyPanel
	 * @param dialog 
	 */
	public ProxyPanel(JDialog dialog) {
		this.parent = dialog;		
		initComponents();
		String host = System.getProperty("http.proxyHost");
		String port = System.getProperty("http.proxyPort");
		String user = System.getProperty("http.proxyUser");
		String pass = System.getProperty("http.proxyPassword");
		if (Helper.isNotEmptyOrNull(host)) {
			tfHost.setText(host);
		}
		if (Helper.isNotEmptyOrNull(port)) {
			tfPort.setText(port);
		}
		if (Helper.isNotEmptyOrNull(user)) {
			tfUser.setText(user);
		}
		if (Helper.isNotEmptyOrNull(pass)) {
			tfPass.setText(pass);
		}
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		tfHost = new javax.swing.JTextField();
		lblHost = new javax.swing.JLabel();
		lblPort = new javax.swing.JLabel();
		lblUser = new javax.swing.JLabel();
		lblPass = new javax.swing.JLabel();
		tfPort = new javax.swing.JTextField();
		tfUser = new javax.swing.JTextField();
		tfPass = new javax.swing.JTextField();
		btnOK = new javax.swing.JButton();
		btnClear = new javax.swing.JButton();

		lblHost.setText("服务器");

		lblPort.setText("端口");

		lblUser.setText("用户名");

		lblPass.setText("密码");

		btnOK.setText("保存设置");
		btnOK.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnOKActionPerformed(evt);
			}
		});

		btnClear.setText("取消代理");
		btnClear.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnClearActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout
						.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								layout
										.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
										.addGroup(
												layout.createSequentialGroup().addComponent(lblHost).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
														.addComponent(tfHost))
										.addGroup(
												layout
														.createSequentialGroup()
														.addGroup(
																layout
																		.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
																		.addGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING,
																				layout.createSequentialGroup().addComponent(lblPort)
																						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
																		.addGroup(
																				layout
																						.createSequentialGroup()
																						.addGroup(
																								layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(lblPass)
																										.addComponent(lblUser)).addGap(10, 10, 10)))
														.addGroup(
																layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(tfPass).addComponent(tfPort)
																		.addComponent(tfUser, javax.swing.GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE)))
										.addGroup(
												javax.swing.GroupLayout.Alignment.TRAILING,
												layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE).addComponent(btnClear)
														.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(btnOK))).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout
								.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout
												.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(lblHost)
												.addComponent(tfHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout
												.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(lblPort)
												.addComponent(tfPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout
												.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(lblUser)
												.addComponent(tfUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout
												.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(lblPass)
												.addComponent(tfPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(btnOK).addComponent(btnClear))
								.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
	}// </editor-fold>

	private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {
		action = JOptionPane.OK_OPTION;
		parent.setVisible(false);
	}

	private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {
		tfHost.setText("");
		tfPort.setText("");
		tfUser.setText("");
		tfPass.setText("");
	}

	public int getAction() {
		return action;
	}

	// Variables declaration - do not modify
	private javax.swing.JButton			btnClear;

	private javax.swing.JButton			btnOK;

	private javax.swing.JLabel			lblHost;

	private javax.swing.JLabel			lblPass;

	private javax.swing.JLabel			lblPort;

	private javax.swing.JLabel			lblUser;

	private javax.swing.JTextField	tfHost;

	private javax.swing.JTextField	tfPass;

	private javax.swing.JTextField	tfPort;

	private javax.swing.JTextField	tfUser;
	// End of variables declaration

	public String getHost() {
		return this.tfHost.getText();
	}

	public String getPort() {
		return this.tfPort.getText();
	}

	public String getUser() {
		return this.tfUser.getText();
	}

	public String getPass() {
		return this.tfPass.getText();
	}
}
